package com.jvxb.manage.livable.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Condition;
import com.jvxb.common.constants.DbConstants;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.manage.livable.entity.AssetsDbPackage;
import com.jvxb.manage.livable.service.AssetsDbPackageService;
import com.jvxb.manage.livable.service.CommonService;
import com.jvxb.manage.livable.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author jvxb
 * @since 2022-01-06
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    SysDictService dictService;
    @Autowired
    AssetsDbPackageService dbPackageService;


    @Override
    public Map getDeployCondition(String dbType) {
        String deployCondition = dictService.getUniValueByGroupAndKey("deploy", dbType, defaultDeployCondition(dbType));
        Map deployConditionMap = JSONObject.parseObject(deployCondition, Map.class);
        return deployConditionMap;
    }

    private Object defaultDeployCondition(String dbType) {
        Map deployInfo = CommonUtil.getMap();
        deployInfo.put("env", CommonUtil.getList(DbConstants.PRD, DbConstants.PRE, DbConstants.DEV, DbConstants.TEST));
        deployInfo.put("idcCode", CommonUtil.getList(DbConstants.IDC_SZ));
        deployInfo.put("archType", CommonUtil.getList(DbConstants.ARCH_TYPE_STANDARD, DbConstants.ARCH_TYPE_CLUSTER));
        deployInfo.put("package", dbPackageService.list(Condition.<AssetsDbPackage>create().eq(AssetsDbPackage.DB_TYPE, dbType)));
        if (dbType.equalsIgnoreCase(DbConstants.MYSQL)) {
            deployInfo.put("version", CommonUtil.getList(DbConstants.VERSION_MYSQL_57, DbConstants.VERSION_MYSQL_56));
            return JSON.toJSONString(deployInfo);
        } else if (dbType.equalsIgnoreCase(DbConstants.REDIS)) {
            deployInfo.put("version", CommonUtil.getList(DbConstants.VERSION_Redis_50, DbConstants.VERSION_Redis_40));
        } else {
            deployInfo.put("version", CommonUtil.getList(DbConstants.UNKONW));
        }
        return JSON.toJSONString(deployInfo);
    }

}
